package com.module_live.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.herewhite.sdk.domain.Appliance;
import com.lib.fastkit.db.shared_prefrences.SharedPreferenceManager;
import com.lib.fastkit.utils.log.LogUtil;
import com.lib.fastkit.utils.share.tool.StringUtil;
import com.lib.fastkit.views.dialog.normal.CoachAIStudyDialog;
import com.lib.fastkit.views.dialog.normal.DaYiRoomBroadDialog;
import com.lib.fastkit.views.dialog.normal.NormalDialog;
import com.lib.ui.fragment.BaseAppFragment;
import com.luck.picture.lib.entity.LocalMedia;
import com.module_live.R;
import com.module_live.R2;
import com.module_live.activity.DaYiConnectRoomActivity;
import com.module_live.bean.OtoControlBean;
import com.module_live.view.BroadListPopup;
import com.zyyoona7.popup.EasyPopup;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2020/7/16 0016.
 */

public class CharControlFragment extends BaseAppFragment {

    @BindView(R2.id.lin_student_hand)
    LinearLayout linStudentHand;
    @BindView(R2.id.lin_student_image)
    LinearLayout linStudentImage;
    @BindView(R2.id.lin_student_move)
    LinearLayout linStudentMove;
    @BindView(R2.id.lin_student_contain)
    LinearLayout linStudentContain;
    @BindView(R2.id.iv_student_pickup)
    ImageView ivStudentPickup;
    @BindView(R2.id.fr_student)
    FrameLayout frStudent;

    @BindView(R2.id.lin_teacher_broad)
    LinearLayout linTeacherBroad;
    @BindView(R2.id.lin_teacher_pptnext)
    LinearLayout linTeacherPpt;
    @BindView(R2.id.lin_teacher_pen)
    LinearLayout linTeacherPen;
    @BindView(R2.id.lin_teacher_eraser)
    LinearLayout linTeacherEraser;
    @BindView(R2.id.lin_teacher_image)
    LinearLayout linTeacherImage;
    @BindView(R2.id.lin_teacher_move)
    LinearLayout linTeacherMove;
    @BindView(R2.id.lin_teacher_exit)
    LinearLayout linTeacherExit;
    @BindView(R2.id.lin_teacher_contain)
    LinearLayout linTeacherContain;
    @BindView(R2.id.iv_teacher_pickup)
    ImageView ivTeacherPickup;
    @BindView(R2.id.fr_teacher)
    FrameLayout frTeacher;

    private String identity = "";
    private String token = "";

    private OtoControlBean roomControlBean;

    private View root;

    //白板列表弹出框和白板ppt地址集合
    BroadListPopup broadListPopup;
    List<String> pptUrlList = new ArrayList<>();

    @Override
    protected void onCreateView(View view, Bundle savedInstanceState) {


        root = view;
        identity = SharedPreferenceManager.getInstance(getActivity()).getUserCache().getUserIdentity();
        token = SharedPreferenceManager.getInstance(getActivity()).getUserCache().getUserToken();

        //通过身份来判断显示不同控制菜单
        if (StringUtil.isNotEmpty(identity, true)) {
            if ("1".equals(identity)) {
                frStudent.setVisibility(View.GONE);
            } else if ("2".equals(identity)) {

                frTeacher.setVisibility(View.GONE);
                frStudent.setVisibility(View.VISIBLE);
            }
        }

        roomControlBean = DaYiConnectRoomActivity.roomControlBean;
        roomControlBean.setDefault_menu(false);

        initIconState();
        initBroadListPopup();

    }

    /**
     * 更新白板列表地址
     *
     * @param pptUrl
     */
    public void updateBroadList(List<String> pptUrl) {
        if (pptUrlList.size() > 0) {
            pptUrlList.clear();
        }

        pptUrlList.addAll(pptUrl);
        broadListPopup.setBroadListUpdate(pptUrlList);
    }

    /**
     * 初始化列表弹出框
     */
    private void initBroadListPopup() {

        broadListPopup = new BroadListPopup(getActivity());
        broadListPopup.setBroadListListener(new BroadListPopup.BroadListListener() {
            @Override
            public void addWhiteBroad() {

                DaYiRoomBroadDialog.getInstance()
                        .setTitle("温馨提示")
                        .setContent("请选择新增白板的类型")
                        .setCancelListener(new DaYiRoomBroadDialog.CancelListener() {
                            @Override
                            public void onCancel() {
                                broadListPopup.dismiss();
                                if (listener != null) {
                                    listener.onAddNoneWhiteBroad();
                                }

                            }
                        })
                        .setSureListener(new DaYiRoomBroadDialog.SurelListener() {
                            @Override
                            public void onSure() {

                                broadListPopup.dismiss();
                                DaYiConnectRoomActivity.whiteBoardFragment.insertPPTImage();
                            }
                        }).show(getFragmentManager());

            }

            @Override//通过白板位置来显示一个ppt白板
            public void showOneBroad(int position) {

                broadListPopup.dismiss();
                if (listener != null) {
                    listener.onWhiteBroadSelected(position);
                }
            }

            @Override//通过位置来删除一个ppt白板
            public void onDeletePPTImage(int position) {

                if (listener != null) {
                    listener.onDeletePPTImage(position);
                }

            }
        });

    }

    private void initIconState() {

    }

    protected int getLayoutId() {
        return R.layout.fragment_room_control;
    }


    @OnClick({R2.id.lin_student_hand,
            R2.id.lin_student_image, R2.id.lin_student_move,
            R2.id.lin_student_exit, R2.id.lin_student_contain, R2.id.iv_student_pickup,
            R2.id.fr_student, R2.id.lin_teacher_broad, R2.id.lin_teacher_pptnext, R2.id.lin_teacher_pen,
            R2.id.lin_teacher_eraser, R2.id.lin_teacher_image, R2.id.lin_teacher_move,
            R2.id.lin_teacher_exit, R2.id.lin_teacher_contain,
            R2.id.iv_teacher_pickup, R2.id.fr_teacher})

    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.lin_student_image) {
            DaYiConnectRoomActivity.whiteBoardFragment.insertImage();

        } else if (i == R.id.lin_student_move) {
            DaYiConnectRoomActivity.whiteBoardFragment.setBoradTool(Appliance.SELECTOR);


        } else if (i == R.id.lin_student_hand) {

            showToast("举手成功！");

            if (listener != null) {//学生举手
                listener.onStudentPuyHand();
            }

        } else if (i == R.id.lin_student_exit) {
//            if (listener != null) {
//                listener.onLeaveRoom();
//            }

        } else if (i == R.id.iv_student_pickup) {
            if (linStudentContain.getVisibility() == View.VISIBLE) {

                linStudentContain.setVisibility(View.GONE);
                ivStudentPickup.setBackgroundResource(R.mipmap.live_icon_spring);

            } else {

                linStudentContain.setVisibility(View.VISIBLE);
                ivStudentPickup.setBackgroundResource(R.mipmap.live_icon_pack);
            }


        } else if (i == R.id.lin_teacher_broad) {

            if (listener != null) {
                listener.onWhiteBroadClick();
            }
            broadListPopup.showAnswerPopuPopu(view);

        } else if (i == R.id.lin_teacher_pptnext) {

            if (listener != null) {
                listener.onMoveToNextBroad();
            }

        } else if (i == R.id.lin_teacher_pen) {
            DaYiConnectRoomActivity.whiteBoardFragment.setBoradTool(Appliance.PENCIL);

        } else if (i == R.id.lin_teacher_eraser) {
            DaYiConnectRoomActivity.whiteBoardFragment.setBoradTool(Appliance.ERASER);

        } else if (i == R.id.lin_teacher_image) {
            DaYiConnectRoomActivity.whiteBoardFragment.insertImage();

        } else if (i == R.id.lin_teacher_move) {
            DaYiConnectRoomActivity.whiteBoardFragment.setBoradTool(Appliance.SELECTOR);

        } else if (i == R.id.lin_teacher_exit) {

            if (listener != null) {
                listener.onLeaveRoom();
            }

        } else if (i == R.id.iv_teacher_pickup) {
            if (linTeacherContain.getVisibility() == View.VISIBLE) {

                linTeacherContain.setVisibility(View.GONE);
                ivTeacherPickup.setBackgroundResource(R.mipmap.live_icon_spring);
            } else {

                linTeacherContain.setVisibility(View.VISIBLE);
                ivTeacherPickup.setBackgroundResource(R.mipmap.live_icon_pack);
            }
        }
    }


    public interface RoomControlFragmentListener {

        void onLeaveRoom();//退出直播间回调

        void onWhiteBroadSelected(int position);//显示一个点中的ppt

        void onWhiteBroadClick();//点击显示白板列表啊

        void onAddNoneWhiteBroad();//添加空白白板

        void onMoveToNextBroad();//移动到下一页PPT

        void onDeletePPTImage(int position);//删除一张ppt

        void onStudentPuyHand();//学生举手
    }


    private RoomControlFragmentListener listener;

    public void setRoomControlFragmentListener(RoomControlFragmentListener roomControlFragmentListener) {

        this.listener = roomControlFragmentListener;

    }


    //=============================================================================================
    //==============================================================================图片选择=======
    //=============================================================================================
    List<LocalMedia> listImage = new ArrayList<>();

    /**
     * 修改麦克风UI
     */
    public void isOpenVoiceUI(boolean b) {

        if (b) {
            roomControlBean.setDefault_voice(true);
        } else {
            roomControlBean.setDefault_voice(false);
        }

        initIconState();
    }

}
